import mixin from './mixin'
import API from '../../api'
// import WorkflowReason from '../../views/orderSystem/workflow/workflow_reason.vue'
// import WorkflowLog from '../../views/orderSystem/workflow/workflow_log'
import _ from 'lodash'
export default {
  mixins: [mixin],
  data() {
    return {
      workflowButtons: {},
      workflowForm: {},
    }
  },
  created() {
    this.loadWorkflow()
  },
  methods: {
    // 加载流程数据
    loadWorkflow() {
      const { query, params } = this.$route
      if (params.id && query.category) {
        API.getWorkFlowButtons({ entityId: params.id, entityCategory: query.category }).then(res => {
          this.workflowButtons = [...res.data, { actionCode: 'BTN-LOG', actionName: '审批日志' }]
        })
      }
    },
    // 点击流程按钮
    onWorkflowActionClick(button) {
      const that = this
      if (button.actionCode === 'BTN-LOG') {
        const { query, params } = this.$route
        this.$Modal.info({
          width: 80,
          okText: '关闭',
          closable: true,
          render: h => {
            // return h(WorkflowLog, {
            //   props: {
            //     id: _.parseInt(params.id),
            //     category: query.category,
            //   },
            // })
          },
        })
      } else if (button.reasonMandatory === true) {
        this.$Modal.confirm({
          loading: true,
          render: h => {
            // return h(WorkflowReason, {
            //   on: {
            //     formRef: data => {
            //       this.workflowForm.formRef = data
            //     },
            //     formData: data => {
            //       this.workflowForm.formData = data
            //     },
            //   },
            // })
          },
          onOk: function() {
            that.workflowForm.formRef.validate(valid => {
              if (valid) {
                that.excuteWorkflowAction(button)
              }
            })
            setTimeout(() => {
              this.buttonLoading = false
            }, 1000)
          },
          onCancel: () => {
            this.workflowForm = {}
          },
        })
      } else {
        // let content = `确认${button.actionName}?`
        // if (button.actionPreMsg) {
        //   content = `<p>${button.actionPreMsg}</p>${content}`
        // }
        // this.confirm({ title: '操作确认', content: content }).then(() => {
        this.excuteWorkflowAction(button)
        // })
      }
    },
    // 请求流程操作
    excuteWorkflowAction(button) {
      const { query, params } = this.$route
      let reason = ''
      if (this.workflowForm.formData) {
        reason = this.workflowForm.formData.reason
      }
      API.postWorkFlowAction({
        entityId: _.parseInt(params.id),
        entityCategory: query.category,
        action: button.actionCode,
        actionReason: reason,
      })
        .then(() => {
          this.onGoBack()
          this.workflowForm = {}
        })
        .catch(() => {
          this.workflowForm = {}
        })
    },
  },
}
